package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import com.xcity.game.worldevent.WorldEvent;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class WorldEventListResponse extends Response {
	
	private List<EventInfo> events;
	
	public static class EventInfo implements Comparable<EventInfo> {
		private int id; // 事件id
		private long time; // 事件发生时间
		private String desc; // 事件描述
		private String icon; // 图标
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public long getTime() {
			return time;
		}
		public void setTime(long time) {
			this.time = time;
		}
		public String getDesc() {
			return desc;
		}
		public void setDesc(String desc) {
			this.desc = desc;
		}
		public String getIcon() {
			return icon;
		}
		public void setIcon(String icon) {
			this.icon = icon;
		}
		@Override
		public int compareTo(EventInfo o) {
			return time >= o.time ? 1 : -1;
		}
	}
	
	public WorldEventListResponse(int serial, List<WorldEvent> refs) {
		super(serial);
		if (refs == null || refs.isEmpty()) {
			events = Collections.emptyList();
		} else {
			events = new ArrayList<EventInfo>(3);
			int today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
			Calendar cal = Calendar.getInstance();
			for (int i = refs.size() - 1; i >= 0; i--) {
				WorldEvent ref = refs.get(i);
				cal.setTimeInMillis(ref.getTime());
				if (today != cal.get(Calendar.DAY_OF_YEAR)) {
					break;
				}
				EventInfo info = new EventInfo();
				info.id = ref.getId();
				info.time = ref.getTime();
				info.desc = ref.getRef().getDesc();
				info.icon = ref.getRef().getIcon();
				events.add(info);
			}
			Collections.sort(events);
		}
	}

	public List<EventInfo> getEvents() {
		return events;
	}

	public void setEvents(List<EventInfo> events) {
		this.events = events;
	}

}
